<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
 * 更改超过三天没有审核的二级域名  每天运行一次
 * @category 360hqb
 * @package  cli
 * @author   clooney<wjj706@126.com>
 */
class Cli_Domain extends CLI
{
	public function main()
	{    
	    $select = DB::select()
			->from('shop_domain')
			->where('verify', '=', '1')
			->and_where('create_time', '<', time()-3*24*3600);

		$iterator = new Db_Iterator($select, 'default', 10);

		while ($iterator->valid())
		{
			foreach ($iterator->current() as $g)
			{
				if ( ! empty($g))
				{    
				    $before_domain = DB::select()
						->from('shop_domain')
						->where('shop_id', '=', $g['shop_id'])
						->where('verify', '=', 2)
						->fetch_all();
					if ( ! empty($before_domain)) // 如果存在已前已通过审核的域名则删除已前已通过审核
					{
				        foreach($before_domain as $v)
						{
						    DB::delete('shop_domain')
								->where('shop_id', '=', $g['shop_id'])
								->and_where('verify', '=', 2)
								->execute();
						}
					}
					DB::update('shop_domain') // 将域名的状态改为通过
						->set(array('verify' => 2)) 
						->where('id', '=', $g['id'])
						->execute();
					//更新shops表中的domain
					DB::update('shops') 
						->set(array('domain' => $g['domain'])) 
						->where('shop_id', '=', $g['shop_id'])
						->execute();
					
					CLI::output("域名ID".$g['id']."更新成功...\n");
				}
			}
			$iterator->next();
		}
		CLI::output("更新完毕...");
	}
}